import { observable, action, toJS } from 'mobx';
import {WeaTableNew } from 'comsMobx';
import {WeaLocaleProvider} from 'ecCom';
import { message } from 'antd';
import * as API from "../../apis/task/taskExecl";
const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;
export default class TaskExportStore {
    @observable tableStore = new TableStore(); // new table
    @observable hasRight = false;
    @observable spinloading = false;
    @observable radioGroupParams={};
    @observable conditionInfo=[];

    doInit(){
        this.getExportCondition();
    }

    @action("内容导出查询条件")
    getExportCondition = () => {
        this.spinloading=true;
        API.getExportCondition().then(result => {
            if(result.api_status){
                this.hasRight = result.hasRight;
                this.conditionInfo = result.conditioninfo;
                this.radioGroupParams = toJS(result.radioGroupParams);
                this.getTableDatas(this.radioGroupParams);
            }
            this.spinloading=false;
        });
    };
    @action("查询条件变化")
    updateRadioGroupParams = (params) => {
        if(typeof(params.userRange)!="undefined" && params.userRange!=toJS(this.radioGroupParams.userRange)){
          this.radioGroupParams.createrid='';
          this.radioGroupParams.principalid='';
          this.radioGroupParams.partnerid='';
          //this.conditionInfo[1].selectLinkageDatas[2].browserConditionParam.replaceDatas = []
        }
        for(let key  in params){
          this.radioGroupParams[key] = params[key];
        }
        let radioGroupParams = this.radioGroupParams;
        this.getTableDatas(radioGroupParams);
    };
    @action("查询任务数据list")
    getTableDatas = (params) => {
        this.spinloading = true;
        params.exporttype = 1;
        API.getExportTableList(params).then(action(res => {
            if (res.api_status) {
                this.tableStore.getDatas(res.datas, 1);
            } else {
                message.error(res.msg || getLabel('508818', '接口调用失败！'))
            }
            this.spinloading = false;
        }));
    };
    //导出
    operateExport = () => {
        let dataKey = this.tableStore.dataKey;
        this.spinloading = true;
        API.getExportColumn({dataKey:dataKey}).then(result => {
            let columns = "";
            let sData = toJS(result.destdatas);
            sData.map(item=>{
                columns+=item.dbField+",";
            });
            if(columns==""){
                message.error(getLabel('508818', '接口调用失败！'))
            }else{
                let params = this.radioGroupParams;
                params.columns = columns;
                API.doExportList(params).then(data => {
                    if (data.url) {
                        window.location.href = `${location.origin}${data.url}`
                    }
                });
            }
            this.spinloading = false;
        });
    };
}